package nps.pay.utils;

import nps.pay.config.NpsConfig;
import okhttp3.FormBody;

import java.util.Iterator;
import java.util.Map;

public class SignUtils {

    /**
     * 拼接
     *
     * @param map
     * @return
     */
    public static String splitJoint(Map<String, String> map) {
        StringBuilder builder = new StringBuilder();
        if (map != null && !map.isEmpty()) {
            Iterator<Map.Entry<String, String>> iterator = map.entrySet().iterator();
            FormBody.Builder formBody = new FormBody.Builder();
            while (iterator.hasNext()) {
                Map.Entry<String, String> entry = iterator.next();
                builder.append(entry.getKey()).append("=").append(entry.getValue()).append("&");
            }
            builder.append(NpsConfig.SIGN_SALT);
        }
        System.out.println("签名串：" + builder.toString());
        return builder.toString();
    }

    /**
     * 签名
     *
     * @param signStr
     * @return
     */
    public static String sign(String signStr) {
        return EncryptUtil.MD5(EncryptUtil.MD5(signStr, 1).toUpperCase() + NpsConfig.SIGN_SALT, 0).toUpperCase();
    }

    /**
     * 签名
     *
     * @param map
     * @return
     */
    public static String sign(Map<String, String> map) {
        return sign(splitJoint(map));
    }

}
